package com.hnucm.shopapp.ui.register;

import android.util.Log;

import com.hnucm.shopapp.bean.BaseBean;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.RequestBody;

public class RegisterPresenter implements RegisterContract.IRegisterPresenter {

    private RegisterContract.IRegisterView registerView;
    private RegisterContract.IRegisterModel registerModel;
    public RegisterPresenter(RegisterContract.IRegisterView registerView){
        this.registerView = registerView;
        this.registerModel = new RegisterModel();
    }
    @Override
    public void addUser(RequestBody body) {
        registerModel.addUser(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseBean<Boolean>>() {
                    @Override
                    public void accept(BaseBean<Boolean> booleanBaseBean) throws Throwable {
                        Log.i("RegisterActivity",booleanBaseBean.toString());
                        registerView.addUserSuccess(booleanBaseBean.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        Log.e("RegisterActivity",throwable.toString());
                        registerView.addUserError(throwable);
                    }
                });
    }
}
